package com.bonc.ioc.demo.utils;

import com.bonc.ioc.demo.base.wrapper.McpRequestWrapper;
import feign.RequestTemplate;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * * @description
 *
 * @author jin.xu
 * @date 12/16/20 2:42 PM
 * @change: 12/16/20 2:42 PM by jin.xu for init
 */
public class HttpParameterUtil {

    /**
     * * 请求解密
     * 
     * @param httpServletRequest:
     * @return: null
     * @since: 1.0.0
     * @Author: jin.xu
     * @Date: 12/16/20 9:06 PM
     * @change: 12/16/20 9:06 PM by jin.xu for init
     */
    public static McpRequestWrapper decryptGetParameter(HttpServletRequest httpServletRequest)
                    throws IOException, ServletException {
        // 转换成代理类
        McpRequestWrapper mcpRequestWrapper = new McpRequestWrapper(httpServletRequest);
        // 如果没有参数，不做处理
        if (null == mcpRequestWrapper.getParameterMap()) {
            return null;
        }

        Map<String, String[]> parameterMap = new HashMap<>(mcpRequestWrapper.getParameterMap());
        // 通过token从redis中获取到传输加密秘钥
        String aesKey = getAesKeyByToken("token");
        for (String key : parameterMap.keySet()) {
            System.out.println("请求的参数为：" + key);
            String[] paramValues = parameterMap.get(key);
            System.out.println("原始参数值为：" + paramValues[0]);
            // 对请求参数进行稍微修改,后续可以做为加密使用
            String[] newParamValue = new String[1];
            newParamValue[0] = AesUtil.decrypt(Base64Util.decode(paramValues[0]), aesKey);
            parameterMap.put(key, newParamValue);
        }
        mcpRequestWrapper.setParameterMap(parameterMap);

        return mcpRequestWrapper;
    }

    /**
     * * 请求加密
     *
     * @param requestTemplate:
     * @return: null
     * @since: 1.0.0
     * @Author: jin.xu
     * @Date: 12/16/20 9:06 PM
     * @change: 12/16/20 9:06 PM by jin.xu for init
     */
    public static void encryptGetParameter(RequestTemplate requestTemplate)
            throws IOException, ServletException {
        Map<String,Collection<String>> paramListMap = requestTemplate.queries();
        Map<String, Collection<String>> newQueries = new HashMap<>();

        // 通过token从redis中获取到传输加密秘钥
        String aesKey = getAesKeyByToken("token");

        for (Map.Entry<String, Collection<String>> entry : paramListMap.entrySet()) {
            String param = entry.getValue().iterator().next();
            param=Base64Util.encode(AesUtil.encrypt(param,aesKey));

            Collection<String> value = new ArrayList<>();
            value.add(param);
            newQueries.put(entry.getKey(), value);
        }
        requestTemplate.queries(null);
        requestTemplate.queries(newQueries);

    }

    /**
     * 模拟登录
     */
    private static Map<String, String> loginInfo = new HashMap();

    static {
        loginInfo.put("token", "123456");
        loginInfo.put("123456", "CxVExRZYIhHTtJLu");
    }

    /**
     * 通过token查询AES秘钥
     *
     * @param tokenId
     * @return
     */
    private static String getAesKeyByToken(String tokenId) {
        String userId = loginInfo.get(tokenId);
        return loginInfo.get(userId);
    }
}
